import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/notice_dialog.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/vo/activities_list_vo.dart';
import 'package:xinxuan/vo/adverts_list_vo.dart';
import 'package:xinxuan/vo/banner_list_vo.dart';
import 'package:xinxuan/vo/categoires_list_vo.dart';
import 'package:xinxuan/vo/common_vo/common_news_item_vo.dart';
import 'package:xinxuan/vo/features_list_vo.dart';
import 'package:xinxuan/vo/news_list_vo.dart';
import 'package:xinxuan/vo/promises_list_vo.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class HomePageController extends GetxController {

  /// 顶部导航栏下边
  var tabIndex = 0.obs;
  /// 分类列表 List<List<CategoiresItem>>
  var categoiresArr = [].obs;
  /// banner 列表
  var bannerArr = <String>[].obs;
  /// 活动列表
  var activitiesArr = <ActivitiesItem>[].obs;
  /// 广告列表
  var advertsArr = <AdvertsItem>[].obs;
  /// 精选好物
  var featuresListVo = FeaturesListVo(data: [], meta: Meta(image: '')).obs;
  int featuresPage = 1;
  /// 首页公告
  var newsArr = <CommonNewsItemVo>[].obs;
  /// 精选好物图标
  var promisesArr = <PromisesItem>[].obs;
  // var subHeight = 0.0.obs;
  // final ScrollController scrollController = ScrollController(
  //   keepScrollOffset: false
  // );

  /// 活动图标的初始位置
  var activityDy = 612.h.obs;

  initPageDate() {
    EasyLoading.show();
    Future.wait([
      getCategoiresList(),
      getBannersList(),
      getActivitiesList(),
      getAdvertsList(),
      getFeaturesList(),
      getNewsList(),
      getPromisesList(),
    ]).then((results){
      categoiresArr.value = results[0] as List<List<CategoiresItem>>;
      List<BannerItem> bannerList = results[1] as List<BannerItem>;
      bannerArr.value = [];
      bannerList.forEach((item) {
        bannerArr.add(item.image!);
      });
      activitiesArr.value = results[2] as List<ActivitiesItem>;
      advertsArr.value = results[3] as List<AdvertsItem>;
      featuresListVo.value = results[4] as FeaturesListVo;
      newsArr.value = results[5] as List<CommonNewsItemVo>;
      promisesArr.value = results[6] as List<PromisesItem>;
      update();
      EasyLoading.dismiss();
    }).catchError((e){
      EasyLoading.dismiss();
    });
  }

  // @override
  // void onReady() {
  //   subHeight.value = 0;
  //   scrollController.addListener(() {
  //     if (scrollController.offset <= 0) {
  //       subHeight.value = 0;
  //       return;
  //     }
  //     if (scrollController.offset <= 146.h + (44 - ScreenUtil().statusBarHeight)) {
  //       subHeight.value = scrollController.offset;
  //     } else {
  //       subHeight.value = 146.h + (44 - ScreenUtil().statusBarHeight);
  //     }
  //   });
  //   initPageDate();
  //   super.onReady();
  // }

  @override
  void onInit() {
    // subHeight.value = 0;
    // scrollController.addListener(() {
    //   if (scrollController.offset <= 0) {
    //     subHeight.value = 0;
    //     return;
    //   }
    //   if (scrollController.offset <= 146.h + (44 - ScreenUtil().statusBarHeight)) {
    //     subHeight.value = scrollController.offset;
    //   } else {
    //     subHeight.value = 146.h + (44 - ScreenUtil().statusBarHeight);
    //   }
    // });
    initPageDate();
    super.onInit();
  }

  /// 获取分类
  Future<List<List<CategoiresItem>>> getCategoiresList() async {
    CategoiresListVo categoiresListVo = await HttpUtilService.getCategoiresList(loading: false);
    List<List<CategoiresItem>> pageArr = [];
    List<CategoiresItem> itemArr = [];
    categoiresListVo.data!.forEach((item) {
      if(itemArr.length == 10) {
        itemArr=[];
      }
      if(itemArr.length == 0) {
        pageArr.add(itemArr);
      }
      itemArr.add(item);
    });
    return pageArr;
  }

  /// 获取banner 列表
  Future<List<BannerItem>> getBannersList() async {
    BannerListVo bannerListVo = await HttpUtilService.getBannersList(loading: false);
    return bannerListVo.data!;
  }
  
  /// 获取活动列表
  Future<List<ActivitiesItem>> getActivitiesList() async {
    ActivitiesListVo activitiesListVo = await HttpUtilService.getActivitiesList(loading: false);
    return activitiesListVo.data!;
  }

  /// 获取首页广告
  Future<List<AdvertsItem>> getAdvertsList() async {
    AdvertsListVo advertsListVo = await HttpUtilService.getAdvertsList(loading: false);
    return advertsListVo.data!;
  }

  /// 获取首页精选好物
  Future<FeaturesListVo> getFeaturesList() async {
    featuresPage = 1;
    FeaturesListVo tempVo = await HttpUtilService.getFeaturesList(page: featuresPage, loading: false);
    return tempVo;
  }

  /// 获取更多精选好物
  onloadFeaturesList() async {
    if (featuresListVo.value.meta!.pagination!.total! <= featuresListVo.value.data!.length) {
      return;
    }
    featuresPage++;
    FeaturesListVo tempVo = await HttpUtilService.getFeaturesList(page: featuresPage, loading: false);
    featuresListVo.value.data!.addAll(tempVo.data!);
    update();
  }

  /// 获取首页公告
  Future<List<CommonNewsItemVo>> getNewsList() async {
    NewsListVo newsListVo = await HttpUtilService.getNewsList(loading: false);
    return newsListVo.data!;
  }

  /// 获取精选好物图标
  Future<List<PromisesItem>> getPromisesList() async {
    PromisesListVo promisesListVo = await HttpUtilService.getPromisesList(loading: false);
    return promisesListVo.data!;
  }

  /// 展示通知详情
  clickNoticeItem({required String title, required String description}) {
    Get.dialog(
      NoticeDialog(
        title: '$title',
        description: '$description',
      ),
      barrierDismissible: false
    );
  }
}